import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86155
 * date: 2024-03-01
 * Time: 19:21
 *
 * @author 数组作业
 */

public class Test {
    //判断该数组中是否存在三个连续奇数
    public static boolean func5(int[] arr){
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]%2 != 0){
                count++;
                if(count == 3){
                    return true;
                }
            }else {
                count = 0;
            }
        }
        return false;
    }
    public static void main(String[] args) {
        int[] array = {1,2,3,5,7,8,9};
        System.out.println(func5(array));

    }
    //多数元素，找出数组中出现超过数组数2/n的元素
    //投票法求解
    public static int func3(int[] arr){
        int ret = arr[0];
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i] == ret){
                count++;
            }else {
                count--;
            }
            if(count == 0){
                ret = arr[i+1];
            }
        }
        return ret;
    }
    public static void main5(String[] args) {
        int[] array = {2,2,1,1,1,2,2};
        System.out.println(func3(array));
        //第一种排序找中间
       // Arrays.sort(array);
        //System.out.println(array[array.length/2]);
    }
    //找出数组中只出现一次的数字//两个书问题
    public static int func2(int[] arr){
        int temp = 0;//或者从arr[0],异或后面的从1开始
        for (int i = 0; i < arr.length; i++) {
            temp = temp ^ arr[i];//通过异或消除一样的数字
        }
        return temp;
    }
    public static void main4(String[] args) {
        int[] array = {1,2,3,2,1};
        int ret = func2(array);
        System.out.println(ret);
    }
    //输入一个数，判断数组中是否有两个数之和等于这个数
    //暴力破解法
    public static int[] func1(int[] arr,int k){
        int[] ret = new int[2];
        for (int i = 0; i < arr.length; i++) {
            for (int j = i+1; j < arr.length; j++) {
                if(arr[i]+arr[j] == k){
                    ret[0] = i;
                    ret[1] = j;
                    return ret;
                }
            }
        }
        return ret;
    }
    public static void main3(String[] args) {
        int [] array = {1,2,3,4,5,6};
        int a = 9;
        int[] ret = func1(array,a);
        System.out.println(Arrays.toString(ret));
    }
    //奇数位于偶数之前
    public static void movenumber(int[] arr){
        int left = 0;
        int right = arr.length - 1;
        while(left < right){
            while (left < right && arr[left] % 2 != 0){
                left++;
            }
            while (left < right && arr[right] % 2 == 0){
                right--;
            }
            int temp = arr[left];
            arr[left] = arr[right];
            arr[right] = temp;
        }
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3,4,5,6,7};
        movenumber(array);
        System.out.println(Arrays.toString(array));
    }
    //{1.2.3.4}变为{2.4.6.8}
    public static int[] mul2(int[] arr){
        int[] temp = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            temp[i] = arr[i]*2;
            //arr[i] *= 2;
        }
        return temp;
    }
    public static void main1(String[] args) {
        int[] array = {1,2,3,4};
        //mul2(array);
        int[] ret = mul2(array);
        System.out.println(Arrays.toString(ret));
    }
}
